//引入样式
require('../../assets/css/normalize.css')
require('../../assets/css/reset.css')
require('../../assets/fonts/iconfont.css');
require('./login.less');

//引入模块
const dom = require('../../utils/dom.js');
const axios = require('axios');

document.addEventListener('DOMContentLoaded', function () {
    //表单验证
    dom.get('.reg-btn').addEventListener('click', async function () {
        let error = dom.get('#error');

        //手机号验证
        let tel = dom.get('#tel').value;
        let reg = /^1[3456789]\d{9}$/i;
        if (reg.test(tel)) {
            error.textContent = '';
        } else {
            error.textContent = '手机号格式有误';
            return; //遇到错误时，停下来
        }

        //密码验证
        let pwd = dom.get('#pwd').value;
        if (pwd.length >= 6) {
            error.textContent = '';
        } else {
            error.textContent = '密码必须写6位数以上';
            return;
        }

        //通过验证，发送请求
        let res = await axios.post('http://fitness.h5.itsource.cn:3701/api/user/login', {
            account: tel,
            password: pwd,
        });

        if (res.data.errno === 0) {
            console.log(res);
            localStorage.setItem('token', res.data.data.token); //将token存储起来，以备后用
            alert('登陆成功!');
            location.href = 'index.html';
        } else {
            alert(res.data.message);
        }
    });
});
